package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.task.Cache;
import com.feicheng.persistence.service.IClientService;
import com.feicheng.wxapi.pay.WxAppPayBean;
import com.feicheng.wxapi.pay.transfer.model.TransactionBill;
import com.feicheng.wxapi.pay.transfer.model.TransferBillsResponse;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    WxAppPayBean wxAppPayBean;

    @Autowired
    HttpSession session;

    @Autowired
    IClientService iClientService;

    @Autowired
    Cache cache;

    @GetMapping("/test/tran/bill")
    public JSONObject tb() {
        JSONObject param = new JSONObject();
        TransferBillsResponse res =
        wxAppPayBean.transferBill(
                "wx74a15fa5b76cb5fe",
                "outBillNo12",
                "",
                "o9cJi7OHGPoVQUn6D291gRjmBlsA",
                50,
                "String transferRemark备注",
                "/tran-bill/"+"1234",
                "String userRecvPerception"
        );
        param.put("res", res);
        return param;
    }

    @Transactional
    @PostMapping("/tran-bill/{orderId}")
    public JSONObject noticeTranBill(
            @RequestHeader("Wechatpay-Nonce") String nonce,
            @RequestHeader("Wechatpay-Signature") String signature,
            @RequestHeader("Wechatpay-Signature-Type") String signatureType,
            @RequestHeader("Wechatpay-Timestamp") String timestamp,
            @RequestHeader("Wechatpay-Serial") String serial,
            @RequestBody String body,
            @PathVariable("orderId") String orderId) {

        JSONObject result = new JSONObject();
        log.info("回调成功-----------------------------------------------------------------");
        log.info(body);
        try {
            synchronized (orderId.toString().intern()) {
                TransactionBill transaction =
                        wxAppPayBean.parseTransferBillNotice(
                                nonce,
                                signature,
                                signatureType,
                                timestamp,
                                serial,
                                body);

                log.info(JSONObject.toJSONString(transaction));

                if (transaction.getOutBillNo().startsWith("CLTQ")) {
//                    xyService.tqAfter(transaction.getOutBillNo(), transaction.getState());
                }


                result.put("code", "SUCCESS");
                result.put("message", "hahaha");

            }
        } catch (Exception e) {
            log.error("支付出现问题", e);
            result.put("code", "ERROR");
            result.put("message", "失败");
        } finally {
            return result;
        }
    }


    @Transactional
    @PostMapping("/notice-url/{orderId}")
    public JSONObject noticeOrderPay(
            @RequestHeader("Wechatpay-Nonce") String nonce,
            @RequestHeader("Wechatpay-Signature") String signature,
            @RequestHeader("Wechatpay-Signature-Type") String signatureType,
            @RequestHeader("Wechatpay-Timestamp") String timestamp,
            @RequestHeader("Wechatpay-Serial") String serial,
            @RequestBody String body,
            @PathVariable("orderId") String orderId) {

        JSONObject result = new JSONObject();
        log.info("回调成功-----------------------------------------------------------------");
        log.info(body);
        try {
            synchronized (orderId.toString().intern()) {
                Transaction transaction =
                        wxAppPayBean.parseNotice(
                                nonce,
                                signature,
                                signatureType,
                                timestamp,
                                serial,
                                body);

                if (Transaction.TradeStateEnum.SUCCESS == transaction.getTradeState()) {

                    result.put("code", "SUCCESS");
                    result.put("message", "成功");
//                    if (transaction.getOutTradeNo().startsWith("BMZY")) {
//                        xyService.payZyBmAfter(orderId);
//                    } else if (transaction.getOutTradeNo().startsWith("BM")) {
//                        xyService.payBmAfter(orderId);
//                    } else if (transaction.getOutTradeNo().startsWith("HY")) {
//                        xyService.payHyAfter(orderId);
//                    } else if (transaction.getOutTradeNo().startsWith("YY")) {
//                        yyService.payYyAfter(orderId);
//                    } else if (transaction.getOutTradeNo().startsWith("CZJL")) {
//                        xyService.payCzAfter(orderId);
//                    } else if (transaction.getOutTradeNo().startsWith("XYYJ")) {
//                        xyService.afterYjin(orderId);
//                    }


                } else {
                    result.put("code", "ERROR");
                    result.put("message", "失败");


                }

            }
        } catch (Exception e) {
            log.error("支付出现问题", e);
            result.put("code", "ERROR");
            result.put("message", "失败");
        } finally {
            return result;
        }
    }

}
